pub fn run(args: Vec<String>){
    // println!("args = {:?}", args);

    let config = Config::from_args(&args).unwrap_or_else(|err|{
        println!("error occur in parse Config:{}",err);
        std::process::exit(0);
    });
    // println!("{}",config.to_string());
    
    let file_str = std::fs::read_to_string(&config.file_path)
        .expect("read content failed");
    let result = search(config.query, &file_str);
    output(&result);
}

fn output(result: &[&str]) {
    for line in result {
        println!("{}",line);
    }
}

fn search<'a>(query: &str, content: &'a str) -> Vec<&'a str> {
    content.lines()
        .filter(|f|f.contains(query))
        // .map(|m|m.to_string())
        .collect()
}
struct Config<'a>{
    query: &'a str,
    file_path: &'a str,
}
impl<'a> Config<'a>{
    fn from_args(args: &[String])->Result<Config, &'static str>{
        if args.len() < 3{
            return Err("请输入正确的参数：grep <query> <filePath>");
        }
        Ok(Config{
            query: args[1].as_str(),
            file_path: args[2].as_str()
        })
    }
}
impl<'a> ToString for Config<'a>{
    fn to_string(&self) -> String {
        format!("config={{query={},file_path={}}}",self.query, self.file_path)
    }
}